var config = require('../../../utils/config.js');
var netutil = require('../../../utils/netutil.js');
var baseutil = require('../../../utils/baseutil.js');
let { WeToast } = require('../../../src/wetoast.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TUserInfo: null,
    passengerList: "",
    selectId: "",
    selectCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tempData = app.globalData.tempData
    app.globalData.tempData = null
    baseutil.log("+++")
    //创建可重复使用的toast实例，并附加到this上，通过this.wetoast访问
    new app.WeToast()

    baseutil.log(tempData)
    var selectId = "";
    if (tempData != null) {

      for (var i = 0; i < tempData.length; i++) {
        var passenger = tempData[i]
        if (!baseutil.arrayContain(selectId.split("-"), passenger.Pid)) {
          if (selectId.length > 0)
            selectId += "-" + passenger.Pid
          else
            selectId += + passenger.Pid
        }
      }
    }
    var selectCount = 0;
    if (selectId.length > 0) {
      selectCount = selectId.split("-").length
    }
    baseutil.log("+++" + selectId)

    this.setData({
      TUserInfo: app.globalData.TUserInfo,
      selectId: selectId,
      selectCount: selectCount
    })

    this.getPassengerList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getPassengerList: function () {
    var THIS = this
    var url = config.getBaseUrl("CommonApi", "QueryFrequentPassenger");
    var Data = new Object();
    Data.SiteId = this.data.TUserInfo.SiteID
    Data.EID = this.data.TUserInfo.EID
    Data.UserID = this.data.TUserInfo.UserID
    Data.Name = ""

    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          var passengerList = res.data.Data.DataPassenger;
          if (passengerList == null || passengerList.length == 0) {
            THIS.setData({ passengerList: null })
          } else {
            var selectId = THIS.data.selectId;
            for (var i = 0; i < passengerList.length; i++) {
              if (baseutil.arrayContain(selectId.split("-"), passengerList[i].Pid)) {
                passengerList[i].isSelected = true;
              } else {
                passengerList[i].isSelected = false;
              }
            }
            THIS.setData({ passengerList: passengerList })
           
          }

        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        }
      },
      function (res) {

      })
  },
  passengerAdd: function () {
    baseutil.openNewPage('../../passenger/passengerAdd/passengerAdd?type='+1+"&isFrom="+'0');
  },
  editPassenger: function (e) {
    var position = parseInt(e.currentTarget.dataset.position)
    app.globalData.tempData = this.data.passengerList[position]
    baseutil.log(app.globalData.tempData)
    baseutil.openNewPage('../../passenger/passengerAdd/passengerAdd?type=' + 0 + "&isFrom=" + '0');
  },

  checkboxChange: function (e) {
    var position = parseInt(e.currentTarget.dataset.position)
    var passenger = this.data.passengerList[position]
    var selectId = this.data.selectId
    if (baseutil.arrayContain(selectId.split("-"), passenger.Pid)) {
      selectId = selectId.replace("-" + passenger.Pid, '').replace(passenger.Pid, '')
    } else {
      if (selectId != null && selectId.length > 0)
        selectId += "-" + passenger.Pid
      else
        selectId += + passenger.Pid
    }
    if (selectId.length>0&&selectId.substring(0,1)=='-'){
      selectId = selectId.substring(1, selectId.length)
    }

    var selectCount = 0;
    if (selectId.length > 0) {
      selectCount = selectId.split("-").length
    }
    this.setData({ selectId: selectId, selectCount: selectCount })
    baseutil.log("+++" + selectId)
  },
  confirm: function () {
    var selectCount = this.data.selectCount
    if (selectCount == 0) {
      return;
    }
    var selectId = this.data.selectId
    var passengerList = this.data.passengerList

    var tempPassengerList = new Array();
    for (var i = 0; i < passengerList.length; i++) {
      if (baseutil.arrayContain(selectId.split("-"), passengerList[i].Pid)) {
        tempPassengerList.push(passengerList[i])
      }
    }
    var pages = getCurrentPages();
    var prePage = pages[pages.length - 2];  //上一个页面
    prePage.updateData(tempPassengerList);
    baseutil.log(tempPassengerList)

    baseutil.returnLastPage();
  }
})